package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"

	"github.com/eiannone/keyboard"
)

func old_fasion() {
	fmt.Println(" - Console - ")
	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Print("-> ")
		userInput, _ := reader.ReadString('\n')
		userInput = strings.Replace(userInput, "\r\n", "", -1)
		if userInput == "quit" {
			break
		} else {
			fmt.Println(userInput)
		}
	}
}

func main() {
	err := keyboard.Open()
	if err != nil {
		log.Fatal(err)
	}

	// 非常奇葩的语法，defer是让defer后面的项目不会立刻执行，而是等函数执行完之后再执行
	// 注意这个函数声明后面有一个()，相当于定义一个匿名函数，然后立刻执行。
	defer func() {
		_ = keyboard.Close()
	}()

	// 这个语法创建MAP
	coffees := make(map[int]string)
	coffees[1] = "Cappucino"
	coffees[2] = "Latte"
	coffees[3] = "Americano"
	coffees[4] = "Mocha"
	coffees[5] = "Espresso"

	fmt.Println("MENU")
	fmt.Println("----")
	fmt.Println("1 - Cappucino")
	fmt.Println("2 - Latte")
	fmt.Println("3 - Americano")
	fmt.Println("4 - Mocha")
	fmt.Println("5 - Espresso")
	fmt.Println("Q - Quit the program")

	for {
		char, _, err := keyboard.GetSingleKey()
		if err != nil {
			log.Fatal(err)
		}

		if char == 'q' || char == 'Q' {
			break
		}

		i, _ := strconv.Atoi(string(char))
		t := fmt.Sprintf("You chose %s", coffees[i])
		fmt.Println(t)

	}

	fmt.Println("Program exited.")
}
